﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace A.HTMlServer.HTML
{
    public class HtmlPart
    {
        private DCommon dc = null;
        public HtmlPart()
        {
            dc = new DCommon();
        }

        public string ToTitle(string Html,string pattern,int i)
        {
            return dc.GetPartHtml(Html, pattern, i);
        }
        public string ToContent(string Html, string pattern, int i)
        {
            string Content = dc.GetPartHtml(Html,pattern, i);
            return dc.RemoveA(Content);
        }
        public string ToIntroduction(string Html, string pattern, int i)
        {
            return dc.GetPartHtml(Html, pattern, i);
        }
        public string ToFrom(string Html, string pattern, int i)
        {
            return dc.GetPartHtml(Html, pattern, i);
        }
        public DateTime ToDate(string Html, string pattern, int i)
        {
            string str = dc.GetPartHtml(Html,pattern, i);
            DateTime dt;
            if (DateTime.TryParse(str, out dt))
            {
                return dt;
            }
            return DateTime.Now;
        }

        /// <summary>
        /// 获取页数
        /// </summary>
        /// <param name="Html"></param>
        /// <returns></returns>
        public List<string> GetCountPage(string Html, string pattern, int i, string Apattern,int Ai)
        {
            List<string> List = new List<string>();
            Html = dc.GetPartHtml(Html, pattern, i);
            if(Html!="")
            {
                System.Text.RegularExpressions.MatchCollection Mc=dc.GetMatchs(Html, Apattern);
                foreach(System.Text.RegularExpressions.Match M in Mc)
                {
                    if (M.Groups.Count>Ai)
                    {
                        if (!List.Contains(M.Groups[Ai].Value))
                        {
                            List.Add(M.Groups[Ai].Value);
                        }
                    }
                }
            }
            return List;
        }

        /// <summary>
        /// 返回是不否匹配
        /// </summary>
        /// <param name="Html"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public bool GetIsMatch(string Html, string pattern)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(Html, pattern);
        }

        public string GetPageContent(string url,string pattern,int i)
        {
            string Html = GetHtml(url);
            if(Html!=null)
            {
               return ToContent(Html,pattern,i);
            }
            return "";
        }

        /// <summary>
        /// 别名
        /// </summary>
        /// <param name="Html"></param>
        /// <param name="pattern"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        public string ToCodeName(string Html, string pattern, int i)
        {
            return dc.GetPartHtml(Html, pattern, i);
        }

        public string GetHtml(string url)
        {
            return dc.GetStrHtml(url);
        }

        public string RemovePart(string Html, string pattern, string strvalue)
        {
            return dc.RemovePart(Html,pattern,strvalue);
        }
    }
}
